import request from "@/utils/request";

// 修改基础URL为正确的服务器地址
const API_BASE_URL = 'http://110.40.135.120:8080';

//获取轮播图
export function plan(data) {
  return request({
    url: "/plan",
    method: "get",
    params: data,
  });
}

export function coach(data) {
  return request({
    url: "/statistics/coach",
    method: "get",
    params: data,
  });
}

///plan-ins/stop/{id}/{izSign}

export function stop(data) {
  return request({
    url: `/plan-ins/stop/${data.id}/${data.izSign}`,
    method: "put",
  });
}
// /plan-ins/start/{id}

export function start(id) {
  return request({
    url: `/plan-ins/start/${id}`,
    method: "put",
  });
}



export function login(data) {
  return request({
    url: "/weixin_mini_app/login",
    method: "post",
    data: data,
  });
}

//詳情
export function qrcode() {
  return request({
      url: '/common/qrcode',
      method: 'get',
  })
}
export function planDetail(id) {
  return request({
    url: `/plan/${id}`,
    method: "get",
  });
}

export function planDetails(id) {
  return request({
    url: `/plan-ins/${id}`,
    method: "get",
  });
}

//获取用户

export function customer(data) {
  return request({
    url: `/customer`,
    method: "get",
    params: data,
  });
}

export function customerAdd(data) {
  return request({
    url: `/customer`,
    method: "post",
    data: data,
  });
}

///customer/{id}

export function customerDetail(id) {
  return request({
    url: `/customer/${id}`,
    method: "get",
  });
}

///physical-fitness

export function fitness(data) {
  return request({
    url: `/physical-fitness`,
    method: "get",
    params: data,
  });
}

///physical-fitness

export function addfitness(data) {
  return request({
    url: `/physical-fitness`,
    method: "post",
    data: data,
  });
}


export function planIns(data) {
  return request({
    url: `/plan-ins`,
    method: "get",
    params: data,
  });
}


export function addPlanIns(data) {
  return request({
    url: `/plan-ins`,
    method: "post",
    data: data,
  });
}

// 添加文件上传接口
export function uploadFile(filePath) {
  return new Promise((resolve, reject) => {
    if (!filePath) {
      reject(new Error('文件路径不能为空'));
      return;
    }

    // 检查token
    const token = uni.getStorageSync('token');
    if (!token) {
      reject(new Error('未登录或token已失效'));
      return;
    }

    uni.uploadFile({
      url: `${API_BASE_URL}/common/upload`,
      filePath: filePath,
      name: 'file',
      header: {
        'content-type': 'multipart/form-data',
        'Authorization': 'Bearer '+token
      },
      success: (uploadFileRes) => {
        try {
          console.log('上传响应:', uploadFileRes);
          const result = JSON.parse(uploadFileRes.data);
          if (result.code === 200) {
            console.log('上传成功:', result);
            resolve(result);
          } else {
            console.error('上传失败:', result);
            reject(new Error(result.msg || '上传失败'));
          }
        } catch (e) {
          console.error('解析响应失败:', e, uploadFileRes);
          reject(new Error('上传响应格式错误'));
        }
      },
      fail: (err) => {
        console.error('上传请求失败:', err);
        if (err.errMsg.includes('fail timeout')) {
          reject(new Error('上传超时，请重试'));
        } else if (err.errMsg.includes('fail network')) {
          reject(new Error('网络错误，请检查网络连接'));
        } else {
          reject(new Error(err.errMsg || '上传失败'));
        }
      },
      complete: () => {
        console.log('上传请求完成');
      }
    });
  });
}

// 添加保存模板接口
export function savePlanTemplate(data) {
  return request({
    url: '/plan',
    method: 'post',
    data: data
  });
}


